﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jumpton.SFA.Server.BusinessRule.MasterData;
using Jumpton.SFA.Server.Entity;
using Jumpton.SFA.Server.BusinessRule;

public partial class HistoryQuery : PageBase
{
    private const string NEW_PAGE = "Customer_Select.aspx";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            txtStart.Text = DateTime.Now.AddMonths(-1).ToString("yyyy-MM-dd");
            txtEnd.Text = DateTime.Now.ToString("yyyy-MM-dd");
            GridPager1.GridView = gvChannelType;
            GridPager1.CurrentPage = PageIndex;
            InitForm();
        }
    }

    private void InitForm()
    {
        DictionaryItemsManager dim = new DictionaryItemsManager();

        //区域层次
        ddlLevel.DataTextField = "Name";
        ddlLevel.DataValueField = "DictionaryItemId";
        ddlLevel.DataSource = dim.GetItemList(DictionaryEnum.ChannelType, this.MyStateManager.DomainID);
        ddlLevel.DataBind();
        ddlLevel.Items.Insert(0, new ListItem("--所有--", ""));
       
        //客户类别

        ddlClass.DataTextField = "Name";
        ddlClass.DataValueField = "DictionaryItemId";
        ddlClass.DataSource = dim.GetItemList(DictionaryEnum.CustomerClass, this.MyStateManager.DomainID);
        ddlClass.DataBind();
        ddlClass.Items.Insert(0, new ListItem("--所有--", ""));

        ////销售代表
        //DataTable dt = new Jumpton.SFA.Server.BusinessRule.MobileUserManager().Load("", this.MyStateManager.DomainID,this.MyStateManager.OrgID);
        //ddlUser.DataTextField = "CODENAME";
        //ddlUser.DataValueField = "ID";
        //ddlUser.DataSource = dt;
        //ddlUser.DataBind();
        //ddlUser.Items.Insert(0, new ListItem("--所有--", ""));

        //销售渠道
        ddlChannel.DataTextField = "Name";
        ddlChannel.DataValueField = "DictionaryItemId";
        ddlChannel.DataSource = dim.GetItemList(DictionaryEnum.CustomerChannel, this.MyStateManager.DomainID);
        ddlChannel.DataBind();
        ddlChannel.Items.Insert(0, new ListItem("--所有--", ""));

        //绑定订单状态
        string[] names = Enum.GetNames(typeof(Jumpton.SFA.Server.Entity.EOderStatus));
        int[] values = (int[])Enum.GetValues(typeof(Jumpton.SFA.Server.Entity.EOderStatus));
        for (int i = 0; i < names.Length; i++)
        {
            ddlStatus.Items.Add(new ListItem(names[i], values[i].ToString()));
        }
        ddlStatus.Items.Insert(0, new ListItem("--所有--", ""));
    }
    protected void SelectALL(object sender, EventArgs e)
    {
        CheckBox chk = sender as CheckBox;
        if (chk == null)
            return;
        foreach (GridViewRow row in this.gvChannelType.Rows)
        {
            CheckBox checkbox = row.Cells[gvChannelType.Columns.Count - 1].FindControl("ckbSelected") as CheckBox;
            if (checkbox != null)
                checkbox.Checked = chk.Checked;
        }
    }

    private void BindGrid()
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder(" ");


        if (txtChains.Text.Trim() != string.Empty)
        {
            sb.Append(" And CHAINS.DESCRIPTION LIKE '%" + txtChains.Text.Trim() + "%' ");
        }
        if (ddlChannel.SelectedValue != string.Empty)
        {
            sb.Append(" AND C.LOCAL_LEVEL1_CODE =  '" + ddlChannel.SelectedValue + "'  ");
        }
        if (ddlClass.SelectedValue != string.Empty)
        {
            sb.Append(" AND C.CUST_CLASS_CODE =  '" + ddlClass.SelectedValue + "'  ");
        }
        if (ddlLevel.SelectedValue != string.Empty)
        {
            sb.Append(" AND C.GEO_LEVEL_CODE ='" + ddlLevel.SelectedValue + "' ");
        }
        //if (ddlUser.SelectedValue != string.Empty)
        //{
        //    sb.Append(" AND OH.USER_ID ='"+ddlUser.SelectedValue+"'  ");
        //}
        if (txtCode.Text != string.Empty)
        {
            sb.Append(" AND (C.CODE LIKE '%"+txtCode.Text+"%' OR C.NAME LIKE '%"+txtCode.Text+"%')  ");
        }
        if (txtStart.Text != string.Empty)
        {
            sb.Append(" AND DATEDIFF(DD,dbo.[GetDateTimeByBigint](OH.ORDER_date),'" + DateTime.Parse(txtStart.Text).ToShortDateString() + "')<=0 ");
        }
        if (txtEnd.Text != string.Empty)
        {
            sb.Append(" AND DATEDIFF(DD,dbo.[GetDateTimeByBigint](OH.ORDER_date),'" + DateTime.Parse(txtEnd.Text).ToShortDateString() + "')>=0 ");
        }
        if (ddlStatus.SelectedValue != string.Empty)
        {
            sb.Append(" AND  OH.STATUS='" + ddlStatus.SelectedValue + "'  ");
        }

        sb.Append(" AND (USER_ID="+MyStateManager.UserID+" or USER_ID in(select ID from USERS where person_id in(select ID from PERSONS where MANAGER_ID="+MyStateManager.PersonID+" ))) ");
        ViewState["ordercondition"] = sb.ToString();
        DataSet ds = OrderHeaderManager.SearchOrderList(MyStateManager.DomainID, sb.ToString());
        if (ds != null && ds.Tables.Count > 0)
        {
            GridPager1.DataSource = ds.Tables[0];
            GridPager1.BindData();
        }


        //this.gvChannelType.DataSource = CustomerManager.SearchCustomer(" AND C.[DOMAIN_ID] = '"+this.MyStateManager.DomainID+"' ");
        //this.gvChannelType.DataBind();
    }

    protected void btnNew_Click(object sender, EventArgs e)
    {
        Response.Redirect(NEW_PAGE);
    }

    protected void btnQuery_Click(object sender, EventArgs e)
    {
        BindGrid();
    }

    public void GridViewToolbarUserControl1_OnGVTBSetColumnsButtonEvent(object sender, EventArgs e)
    {
        BindGrid();
    }
    protected void btnExport_Click(object sender, EventArgs e)
    {
        string orderID = "";
        bool flag = false;
        for (int i = 0; i < gvChannelType.Rows.Count; i++)
        {
            orderID = gvChannelType.DataKeys[i].Value.ToString();
            CheckBox ckbSelect = gvChannelType.Rows[i].FindControl("ckbSelected") as CheckBox;

            if (orderID != "" && ckbSelect != null && ckbSelect.Checked)
            {
                OrderHeaderManager.ExportOrderToHistory(orderID, MyStateManager.Name, MyStateManager.DomainID);
                flag = true;
            }
        }
        this.BindGrid();
        if(flag)
            ShowMessage("导出成功！");
    }
    protected void Grid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //if (e.Row.RowType == DataControlRowType.DataRow)
        //{
        //    LinkButton lbtn_looksign = e.Row.FindControl("lbtn_looksign") as LinkButton;
        //    if (lbtn_looksign != null)
        //    {
        //        if (lbtn_looksign.CommandArgument != "")
        //        {
        //            System.Text.StringBuilder sb = new System.Text.StringBuilder("");
        //            sb.Append("javascript:window.showModalDialog('../CallRoute/ReadPhoto.aspx?id=");
        //            sb.Append(lbtn_looksign.CommandArgument);
        //            sb.Append("','','dialogHeight:660px;dialogWidth:660px;left='+(screen.width-100)/2+',top='+(screen.height-100)/2+',center:yes;help:no;status:no;toolbar:no;');return false;");
        //            lbtn_looksign.Attributes.Add("onclick", sb.ToString());
        //        }
        //        else
        //        {
        //            lbtn_looksign.Visible = false;
        //        }
        //    }
        //}
    }
    protected void btnExportExcel_Click(object sender, EventArgs e)
    {
        if (ViewState["ordercondition"] != null)
        {
            Session["ordercondition"]=ViewState["ordercondition"];
        }
        Response.Redirect(string.Format(@"..\ExportExcel\ExportExcel.aspx?Table=OrderReport"));
    }
}
